/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ListIterator;

import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.crossSection.CrossSection;
import br.ufmg.dees.insane.materialMedia.material.LinearElasticIsotropic;
import br.ufmg.dees.insane.materialMedia.material.Material;
import br.ufmg.dees.insane.model.disc.analysismodel.AnalysisModel;
import br.ufmg.dees.insane.model.disc.analysismodel.AxiSymetricAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.Beam;
import br.ufmg.dees.insane.model.disc.analysismodel.Grid;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneAnalysisM;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneFrame;
import br.ufmg.dees.insane.model.disc.analysismodel.PlaneTruss;
import br.ufmg.dees.insane.model.disc.element.Element;
import br.ufmg.dees.insane.model.disc.element.ParametricElement;
import br.ufmg.dees.insane.model.disc.fem.FemModel;
import br.ufmg.dees.insane.model.disc.integrationorder.IntegrationOrder;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since May 2004
 * @see gui.command.Command
 */
public class CheckDataInteractiveCommand implements Command {
    
    private InsaneInternalInterface parent;
	private FemModel fmodel;
		
	private boolean showOKDialog = true;
	private boolean dataOk = true;
	
	private boolean nullMatError;
	private boolean nullCsError;
	
	private boolean areaPFError;
	private boolean ePFError;
	private boolean izPFError;
	
	private boolean eBError;
	private boolean izBError;
	
	private boolean areaPTError;
	private boolean ePTError;
	
	private boolean eGError;
	private boolean gGError;
	private boolean ixGError;
	private boolean izGError;
	
	private boolean thickParError;
	private boolean eParError;
	//private boolean poissonParError;
	
	private boolean nullGaussPointError;
	
	private boolean dataOK = false;
//*****************************************************************************
	
    /**
     * Creates new CheckDataCommand
     *
     * @see gui.command.Command#execute()
     * @param parent The JInternalFrame parent.
     */
    public CheckDataInteractiveCommand(JInternalFrame parent) {
        this.parent = (InsaneInternalInterface)parent;
    }
    
//*****************************************************************************
	
	public void setDataOK(boolean a){
		dataOK = a;
	}
	
//*****************************************************************************
	
	public boolean getDataOK(){
		return dataOK;
	}
	
	
//*****************************************************************************
	
    public void execute() {
		
		fmodel = this.parent.getModel().getDiscreteModel();
		
		
		//	controller = (ProcessorController)this.parent.getController();
		
		
		dataOk = true;
		nullMatError = false;
		nullCsError = false;
		areaPFError = false;
		ePFError = false;
		izPFError = false;
		eBError = false;
		izBError = false;
		areaPTError = false;
		ePTError = false;
		eGError = false;
		gGError = false;
		ixGError = false;
		izGError = false;
		
		thickParError = false;
		eParError = false;
		//poissonParError = false;
		nullGaussPointError = false;
		
		
		AnalysisModel anl;
		Material mat;
		CrossSection cs;
		IntegrationOrder io;
		
		ListIterator elms = fmodel.getElementsList().listIterator();
		while (elms.hasNext()) {
			Element elm = (Element)elms.next();
			anl = elm.getAnalysisModel();
			mat = elm.getMaterial();
			cs = elm.getCrossSection();
			
			
			if (mat == null) {
				nullMatError = true;
				dataOk = false;
			}
			if (cs == null) {
				nullCsError = true;
				dataOk = false;
			}
			
			if (mat!=null && cs!=null) {
				
			    LinearElasticIsotropic mat2 = new LinearElasticIsotropic();
				if (mat instanceof LinearElasticIsotropic)
					mat2 = (LinearElasticIsotropic)mat;
				
				if (anl instanceof PlaneFrame) {
					if (cs.getArea()==0) {
						areaPFError = true;
						dataOk = false;
					}
					if (cs.getIz()==0) {
						izPFError = true;
						dataOk = false;
					}
					if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
						ePFError = true;
						dataOk = false;
					}
				} else if (anl instanceof Beam) {
					if (cs.getIz()==0) {
						izBError = true;
						dataOk = false;
					}
					if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
						eBError = true;
						dataOk = false;
					}
				} else if (anl instanceof PlaneTruss) {
					if (cs.getArea()==0){
						areaPTError = true;
						dataOk = false;
					}
					if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
						ePTError = true;
						dataOk = false;
					}
				} else if (anl instanceof Grid) {
					if (cs.getIx()==0){
						ixGError = true;
						dataOk = false;
					}
					if (cs.getIz()==0){
						izGError = true;
						dataOk = false;
					}
					if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
						eGError = true;
						dataOk = false;
					}
					if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.SHEAR_MODULUS).doubleValue()==0){
						gGError = true;
						dataOk = false;
					}
				}
				
				if (elm instanceof ParametricElement) {
					
				io = ((ParametricElement)elm).getIntegrationOrder();	
				
				if (io == null) {
					nullGaussPointError = true;
					dataOk = false;
				}
								
					if (anl instanceof AxiSymetricAnalysisM) {
						
												
						if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
							eParError = true;
							dataOk = false;
						}
						//if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.POISSON).doubleValue()==0){
							//poissonParError = true;
							//dataOk = false;
						//}
					}
					
					if (anl instanceof PlaneAnalysisM) {
						
						//cs.setThickness(1);
												
						if (cs.getThickness()==0){
							thickParError = true;
							dataOk = false;
						}
						if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.ELASTICITY).doubleValue()==0){
							eParError = true;
							dataOk = false;
						}
						//if (mat instanceof LinearElasticIsotropic && mat2.getMaterialValues(mat2.POISSON).doubleValue()==0){
							//poissonParError = true;
							//dataOk = false;
						//}
					}
					
				}
				
			}
		}
		
		if (dataOk) {
			if (showOKDialog)
				JOptionPane.showMessageDialog(parent, "The Interactive Processor is a didatic tool, and should be used for models of a few elements.", "Data Check Results", JOptionPane.INFORMATION_MESSAGE);
			
				this.setDataOK(true);
			
			showOKDialog = true;
		} else {
			String str = "The following errors were found:\n\n";
			
			if (nullMatError)
				str = str + "Some elements have null material.\n";
			if (nullCsError)
				str = str + "Some elements have null cross section.\n";
			if (areaPFError)
				str = str + "Some plane frame elements have null area.\n";
			if (ePFError)
				str = str + "Some plane frame elements have null modulus of elasticity.\n";
			if (izPFError)
				str = str + "Some plane frame elements have null Iz.\n";
			if (eBError)
				str = str + "Some beam elements have null modulus of elasticity.\n";
			if (izBError)
				str = str + "Some beam elements have null Iz.\n";
			if (areaPTError)
				str = str + "Some plane truss elements have null area.\n";
			if (ePTError)
				str = str + "Some plane truss elements have null elasticity.\n";
			if (eGError)
				str = str + "Some grid elements have null elasticity.\n";
			if (gGError)
				str = str + "Some grid elements have null G.\n";
			if (ixGError)
				str = str + "Some grid elements have null Ix.\n";
			if (izGError)
				str = str + "Some grid elements have null Iz.\n";
			if (thickParError)
				str = str + "Some parametric elements have null thickness.\n";
			if (eParError)
				str = str + "Some parametric elements have null elasticity.\n";
			//if (poissonParError)
				//str = str + "Some parametric elements have null modulud of Poisson.\n";
			if (nullGaussPointError)
				str = str + "Some parametric elements have null gauss Point.\n";
			
			JOptionPane.showMessageDialog(parent, str, "Data Check Results", JOptionPane.ERROR_MESSAGE);
			
				this.setDataOK(false);
			
			
		}
		
	}
	
//*****************************************************************************
	
    public void undo() {}
	
//*****************************************************************************
	
    public void redo() {}
    
//*****************************************************************************
	
	public void setShowOKDialog(boolean a) {
		showOKDialog = a;
	}
	
//*****************************************************************************
}
